package com.kerrykidz.workflow.service.impl;


import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.MailUtil;
import com.kerrykidz.workflow.dao.mapper.HrmLeaveEntityMapper;
import com.kerrykidz.workflow.dao.model.HrmLeaveEntity;

/**
 * 流程结束发送邮件提醒
 * @author zhuss
 *
 */
@Component
@Transactional
public class LeaveSendMail implements TaskListener {
    private static final long serialVersionUID = 1L;
    
    @Autowired
	HrmLeaveEntityMapper hrmLeaveEntityMapper;
    
    public void notify(DelegateTask delegateTask){
    	if(null == delegateTask.getVariable("isBack"))
    		delegateTask.setVariable("isBack",false);
    	if(!(Boolean)delegateTask.getVariable("isBack")){
			try {
				HrmLeaveEntity hle = hrmLeaveEntityMapper.findEntityById(delegateTask.getExecution().getBusinessKey());
				String subject = "【请假申请】--申请审批通过提醒";
	    		String text = "<html><head></head><body>"
	    				+ "您好！<br><br>"
						+ "申请人提交的【请假申请】已被审批通过，相关信息如下，请您知悉。<br><br>"
	    				+"申请人：【"+hle.getUsrName()+"】<br>"
	    				+"门店：【"+hle.getBranName()+"】<br>"
	    				+"申请单据编号：【"+hle.getPkLeaveId()+"】<br>"
	    				+"请假类型：【"+hle.getLveTypeDesc()+"】<br>"
	    				+"请假开始时间：【"+DateUtil.formatDate("yyyy-MM-dd HH:mm", hle.getLveStart())+"】<br>"
	    				+"请假结束时间：【"+DateUtil.formatDate("yyyy-MM-dd HH:mm", hle.getLveEnd())+"】<br>"
	    				+"<br><br>"
	    				+"请点击此链接查看审批详情：<a href='http://10.20.59.31:8084/kerrykidz/login.shtml'>嘉童儿童乐园</a><br><br><br>"
	    				+"------------------------------------------------------------------<br>"
	    				+"<b>本邮件为系统自动发出，不必回复。</b>"
						+ "</body></html>";
	    		MailUtil.sendHTMLMail(hle.getEmail(),subject, text);
			} catch (Exception e) {
				
			}
    	}
    	delegateTask.setVariable("isBack", false);
    }
}
